//
//  StatusCell.swift
//  微博个人swift
//
//  Created by 李灏 on 16/2/19.
//  Copyright © 2016年 lh. All rights reserved.
// 自定义cell 把cell拆分为几部分来自定义再组合起来

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    //添加约束模型 记录约束用来规范约束
    var bottomViewTopConstaint:Constraint?
    //添加模型属性
    var status:Status? {
        didSet {
            //设置原创微博的模型属性
            statusCellOriginaView.status = status
            //卸载约束
            bottomViewTopConstaint?.uninstall()
            //根据数据判断约束
            if let re = status?.retweeted_status {
                //数据关联
                statusRetweetedView.statusRetweeted = re
                statusCellBottomView.snp_makeConstraints { (make) -> Void in
                    self.bottomViewTopConstaint = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                }
                //显示转发微博视图  不能够被计算为 CGSizeZero  如果不做隐藏操作 会导致数据显示错乱
                statusRetweetedView.hidden = false
            }else{
                //没转发微博时候
                statusCellBottomView.snp_makeConstraints { (make) -> Void in
                    self.bottomViewTopConstaint = make.top.equalTo(statusCellOriginaView.snp_bottom).constraint
                }
                statusRetweetedView.hidden = true
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        //设置cell 选中样式
        selectionStyle = .None
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //更新UI
    private func setupUI() {
        contentView.addSubview(statusCellOriginaView)
        contentView.addSubview(statusCellBottomView)
        contentView.addSubview(statusRetweetedView)
        
        
        statusCellOriginaView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
        }
        statusCellBottomView.snp_makeConstraints { (make) -> Void in
            self.bottomViewTopConstaint = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //设置高度
            make.height.equalTo(35)
        }
        statusRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusCellOriginaView.snp_bottom)
            make.left.equalTo(statusCellOriginaView.snp_left)
            make.right.equalTo(statusCellOriginaView.snp_right)
        }
        
        //给contentView添加约束 为了自动计算行高
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            //最关键的约束
            make.bottom.equalTo(statusCellBottomView.snp_bottom)

        }
    }
    //懒加载转发视图
    private lazy var statusRetweetedView:StatusRetweetedView = StatusRetweetedView()
    
    //懒加载顶部视图
    private lazy var statusCellOriginaView:StatusCellOriginalView = StatusCellOriginalView()
    
    //底部视图
    private lazy var statusCellBottomView: StatusCellBottomView = StatusCellBottomView()
    

    
}
